package mahjong.auto.com.automahjong;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.DataOutputStream;
import java.io.OutputStream;

import mahjong.auto.com.os.Mahjong;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int CODE_FOR_WRITE_PERMISSION = 8;
    private static final int REQUEST_MEDIA_PROJECTION = 9;
    public static Bitmap bitmap = null;
    private Permission permission;
    private MediaProjectionManager mMediaManager;
    private App app;
    private TextView textShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button_click).setOnClickListener(this);
        findViewById(R.id.button_s).setOnClickListener(this);
        findViewById(R.id.button_shot).setOnClickListener(this);
        findViewById(R.id.button_start).setOnClickListener(this);

        textShow = (TextView) findViewById(R.id.text_show);
        app = (App) getApplication();

        if (null == app.getCapture())
            app.setCapture(new ScreenCapture(this));

        isPermission();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != bitmap)
            ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
    }

    public void isPermission() {
        permission = new Permission(this, CODE_FOR_WRITE_PERMISSION);
        permission.addPermission(
                Manifest.permission.SYSTEM_ALERT_WINDOW,
                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        );

        if (!permission.isCheck()) {
            permission.request();
        } else {

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == CODE_FOR_WRITE_PERMISSION) {
            if (permission.isCheck(permissions, grantResults)) {

            }
        }
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        app.getCapture().onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_shot:

                break;

            case R.id.button_start:
                button();

                break;

            case R.id.button_click:
                execShellCmd("input tap 300 252");
                break;

            case R.id.button_s:

                break;
        }
    }

    private void button() {
        new InfoActivity(this);
        Bitmap bitmap = Bitmap.createBitmap(1280, 720, Bitmap.Config.ARGB_8888);

        Mahjong.test(bitmap);
        ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
    }

    private void execShellCmd(String cmd) {

        try {
            // 申请获取root权限，这一步很重要，不然会没有作用
            Process process = Runtime.getRuntime().exec("su");
            // 获取输出流
            OutputStream outputStream = process.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }


}
